<?php

/**
 * Page numbers start from 1, not 0
 *
 */
class SimplePagination
{
	var $total = 0;
	var $recordsPerPage = 30;
	function SimplePagination( $total = null, $recordsPerPage = null )
	{
		if ( $total ) $this->total = $total;
		if ( $recordsPerPage ) $this->recordsPerPage = $recordsPerPage;
	}
	function  getLimit ( $pageNumber = 0 )
	{
		$bounds = $this->getPageBounds( $pageNumber );
		return "LIMIT {$bounds['min']}, {$bounds['max']}";
	}
	function count()
	{
		return  ceil ($this->total / $this->recordsPerPage );
	}
	function getPagesList ()
	{
		$list = array();
		$count = $this->count();
		if ( !$count ) return $list;
		return range( 1, $count );
	}
	function  getPageBounds ( $pageNumber = 0 )
	{
		$min = 0;
		$max = 0;
		if ( $pageNumber )
		{
			$min = ( $pageNumber - 1 )  * $this->recordsPerPage;
			$max = ( $min + $this->recordsPerPage - 1 );
		}
		
		return array( 'min' => $min, 'max' => $max );
	}
	function changeLink ( $nameOfPageParameter, $pageNumber, $href = false )
	{
		return $this->_url_change_params( array ($nameOfPageParameter => $pageNumber), $href );
	}
	function _url_change_params ( $params, $url = false )
	{
		//apply defaults
		if ( $url === false ) $url = $_SERVER['REQUEST_URI'];
		
		//detect what to unset
		$unsetParams = array();
		foreach ( $params as $key => $par )
		{
			if ( $par === false ) $unsetParams[] = $key;
		}
		
		$uriTokens = parse_url( $url );
		$query = (isset( $uriTokens[ 'query' ] ))? $uriTokens[ 'query' ] : '';
		$tokens = array();
		parse_str( $query, $tokens);
		$tokens = array_merge( $tokens, $params );
		foreach ( $unsetParams as $unsetThat ) if ( isset ( $tokens[ $unsetThat ])) unset ( $tokens[ $unsetThat ]);
		if ( $query )
		{
			$url = str_replace( $query, http_build_query( $tokens, '', '&amp;' ), $url );
		} else {
			$url .= '?'.http_build_query( $tokens, '', '&amp;' );
		}
		return $url;
	}
	
}
//------------ COMPAT

if (!function_exists('http_build_query')) {
    function http_build_query($formdata, $numeric_prefix = null, $force_separator = null )
    {
        // If $formdata is an object, convert it to an array
        if (is_object($formdata)) {
            $formdata = get_object_vars($formdata);
        }

        // Check we have an array to work with
        if (!is_array($formdata)) {
            user_error('http_build_query() Parameter 1 expected to be Array or Object. Incorrect value given.',
                E_USER_WARNING);
            return false;
        }

        // If the array is empty, return null
        if (empty($formdata)) {
            return;
        }

        // Argument seperator
        $separator = ini_get('arg_separator.output');
        if ( !is_null( $force_separator ))
        {
        	$separator = $force_separator;
        }
        // Start building the query
        $tmp = array ();
        foreach ($formdata as $key => $val) {
            if (is_integer($key) && $numeric_prefix != null) {
                $key = $numeric_prefix . $key;
            }

            if (is_scalar($val)) {
                array_push($tmp, urlencode($key).'='.urlencode($val));
                continue;
            }

            // If the value is an array, recursively parse it
            if (is_array($val)) {
                array_push($tmp, __http_build_query($val, urlencode($key), $force_separator));
                continue;
            }
        }

        return implode($separator, $tmp);
    }

    // Helper function
    function __http_build_query ($array, $name, $force_separator = null)
    {
        $tmp = array ();
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                array_push($tmp, __http_build_query($value, sprintf('%s[%s]', $name, $key), $force_separator));
            } elseif (is_scalar($value)) {
                array_push($tmp, sprintf('%s[%s]=%s', $name, urlencode($key), urlencode($value)));
            } elseif (is_object($value)) {
                array_push($tmp, __http_build_query(get_object_vars($value), sprintf('%s[%s]', $name, $key), $force_separator));
            }
        }

        // Argument seperator
        $separator = ini_get('arg_separator.output');
         if ( !is_null( $force_separator ))
        {
        	$separator = $force_separator;
        }
        return implode($separator, $tmp);
    }
}



?>